package com.ducbt.baitap4;

import java.util.Locale;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class MainActivity extends Activity {

	EditText etAddress,etContent,etSubject;
	Button btCL;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		etAddress = (EditText)findViewById(R.id.etAddress);
		etContent = (EditText)findViewById(R.id.etContent);
		etSubject = (EditText)findViewById(R.id.etSubject);
		btCL = (Button)findViewById(R.id.btCL);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void callNumber(View v){
		if(etAddress.getText().toString().trim().equals("")){
			Toast.makeText(this, "You need enter a phone number!", Toast.LENGTH_SHORT).show();
		}
		else{
			Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+etAddress.getText().toString()));
			this.startActivity(call);
		}
	}

	public void sendSMS(View v){
		if(etContent.getText().toString().trim().equals("")){
			Toast.makeText(this, "You need enter body of sms!", Toast.LENGTH_SHORT).show();
		}
		else{
			if(etAddress.getText().toString().trim().equals("")){
				Toast.makeText(this, "You need enter a phone number!", Toast.LENGTH_SHORT).show();
			}
			else{
				SmsManager sms = SmsManager.getDefault();
				sms.sendTextMessage(etAddress.getText().toString(), null, etContent.getText().toString(), null, null);
			}
		}
	}
	public void sendEmail(View v){
		if(etContent.getText().toString().trim().equals("")){
			Toast.makeText(this, "You need enter body of email!", Toast.LENGTH_SHORT).show();
		}
		else{
			if(etAddress.getText().toString().trim().equals("")){
				Toast.makeText(this, "You need enter a phone number!", Toast.LENGTH_SHORT).show();
			}
			else{
				if(etSubject.getText().toString().trim().equals("")){
					Toast.makeText(this, "Enter Subject!", Toast.LENGTH_SHORT).show();
				}
				else{
					Intent send  = new Intent(Intent.ACTION_SEND);
					send.setType("text/plain");
					send.putExtra(Intent.EXTRA_EMAIL, etAddress.getText().toString());
					send.putExtra(Intent.EXTRA_SUBJECT, etSubject.getText().toString());
					send.putExtra(Intent.EXTRA_TEXT, etContent.getText().toString());
					try{
						this.startActivity(Intent.createChooser(send, "Send Email"));
					}
					catch(Exception e){
						Toast.makeText(this, "Error!\n"+e.toString(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		}
	}
	public void gotoDantri(View v){
		Intent gotoWeb = new Intent(Intent.ACTION_VIEW, Uri.parse("http://dantri.com.vn"));
		this.startActivity(gotoWeb);
	}
	public void changedLanguage(View v){
		Locale locale = new Locale("vi");
		if(btCL.getText().toString().trim().equalsIgnoreCase("Việt Nam")){
			locale = new Locale("uk");
		}
		if(btCL.getText().toString().trim().equalsIgnoreCase("English")){
			locale = new Locale("vi");
		}
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
		this.setContentView(R.layout.activity_main);
	}
}
